/**
 * 20220114:由原SelectObjectConfig升级，兼容XR模式
 * 20220304:[require] to [import]...jordia
 */
import {Object3D,Mesh}from 'three';
import SelectMeshConfig from "./select-mesh-config";

/**
 * 选取Object3D的配置
 */
export default class SelectMeshGroupConfig extends SelectMeshConfig{
    constructor(values={}){
        super(values);
    }
    get type(){
        return 'SelectMeshGroupConfig';
    }
    checkType(object) {
        if(!object instanceof Object3D)return false;
        let mCount=0;
        object.traverse((c)=>{
            if(c instanceof Mesh)mCount++;
        });
        return mCount>0;
    }

    /**
     * 检测所有子级（含自身）的投影几何体
     */
    intersectRect(object,pointer1,pointer2,camera){
        if(!object.visible)return false;
        let box=this._computeNDCBox(camera,pointer1,pointer2);
        let sus=false;
        object.traverse((c)=>{
            if(!sus){
                if(c instanceof Mesh){
                    sus=this.checkProjectionGeometry(c,box,camera);
                    if(sus)return true;
                }
            }
        });
        return sus;
    }

    /**
     * 将射线检测所得对象（mesh）替换为被检测集合（object3D）
     */
    intersectPointer(object,raycaster,info){
        if(!object.visible)return false;
        this._raycaster=raycaster;
        this.tA0.length=0;
        this._raycaster.intersectObject(object, true,this.tA0);
        for(let i of this.tA0){
            i.object=object;
            i.object.userData.intersectDetail=i;
        }
        info.push(...this.tA0);
        return info;
    }

    dispose() {
        super.dispose();
        this.tA0=null;
    }

    _initVariable() {
        super._initVariable();
        this.tA0=[];
    }
}